package com.haoqizhe.kernel.activemq.connection;

import com.haoqizhe.kernel.activemq.properties.ActivemqProperties;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;
import org.springframework.jms.connection.CachingConnectionFactory;

import javax.jms.ConnectionFactory;

/**
 *
 * 注册带session缓存的连接工厂
 *
 * 1、ActiveMQ自身即有相关的实现PooledConnectionFactory，其只会缓存connection，session和productor，不会缓存consumer，故其适用于生产者。
 * 2、而spring基于JMS标准进行了连接池的实现，故其不仅仅适用于ActiveMQ，并且还提供了对consumer的缓存实现。
 *  Spring定义了CachingConnectionFactory和SingleConnectionFactory，其均有缓存功能，
 *  但考虑到CachingConnectionFactory类扩展自SingleConnectionFactory，其有了更多的实现。
 *
 *  因此本地通过CachingConnectionFactory实现带有缓存池的连接工厂
 *
 * @author  haoqizhe.li on 2017/6/12.
 */
@Configuration
@EnableConfigurationProperties({ActiveMQProperties.class, ActivemqProperties.class})
@Import({ActiveMQXAConnectionFactoryConfiguration.class})
public class ActiveMQConnectionFactoryConfiguration {

    @Autowired
    @Qualifier("jmsConnectionFactory")
    private ConnectionFactory connectionFactory;


    /**
    * 创建带有session缓存的连接工厂
    * @author haoqizhe.li
    * @date 2017/6/12 22:26
    **/
    @Bean(name="cachingConnectionFactory")
    @Primary
    public CachingConnectionFactory getConnectionFactory(ActivemqProperties properties){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

        /**
         *  目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory
         */
        cachingConnectionFactory.setTargetConnectionFactory(connectionFactory);
        /**
         *  session缓存数量
         */
        cachingConnectionFactory.setSessionCacheSize(properties.getSessionCacheSize());

        if(properties.getClientId() != null && !"".equals(properties.getClientId())) {
            cachingConnectionFactory.setClientId(properties.getClientId() + ".send");
        }

        cachingConnectionFactory.setCacheProducers(true);

        return cachingConnectionFactory;
    }

    @Bean(name="cachingReceiveConnectionFactory")
    public CachingConnectionFactory getReceiveConnectionFactory(ActivemqProperties properties){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();

        /**
         *  目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory
         */
        cachingConnectionFactory.setTargetConnectionFactory(connectionFactory);
        /**
         *  session缓存数量
         */
        cachingConnectionFactory.setSessionCacheSize(properties.getSessionCacheSize());

        if(properties.getClientId() != null && !"".equals(properties.getClientId())){
            cachingConnectionFactory.setClientId(properties.getClientId()+".receive");
        }


        cachingConnectionFactory.setCacheConsumers(true);

        return cachingConnectionFactory;
    }


}
